package com.fenghuajueli.libbasecoreui.feedback;

import android.content.Context;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.fenghuajueli.libbasecoreui.baishanyun.Bsybean;
import com.fenghuajueli.libbasecoreui.utils.CommonUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

public class ImageTools {
    private  static AmazonS3 s3;

    public static void initBsy(){
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(
                Bsybean.accessKey, Bsybean.secretKey);

        ClientConfiguration clientconfiguration = new ClientConfiguration();
        clientconfiguration.setSocketTimeout(60 * 60 * 1000); // in milliseconds
        clientconfiguration.setConnectionTimeout(60 * 60 * 1000); // in milliseconds

        s3 = new AmazonS3Client(awsCreds, clientconfiguration);
        s3.setRegion(Region.getRegion(Regions.US_EAST_1));
        s3.setEndpoint(Bsybean.endPoint);
        s3.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true)
                .disableChunkedEncoding().build()); //暂不支持ChunkedEncoding
    }

    public static String getUrl(Context context,String path){
        InputStream inputStream = null;
        try {
            inputStream = new ByteArrayInputStream(readStream(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType("image/jpeg"); //请替换为合适的文件类型
        objectMetadata.addUserMetadata("key-foo", "value_bar");
        String fileid = CommonUtils.getSixtenFourNumber(64);
        String key = "an-static/"+ CommonUtils.getBaketName(context)+"/feedback/"+fileid+".png";
        System.out.println("key======="+key);

        PutObjectRequest putObjectrequest = new PutObjectRequest(
                Bsybean.bucketName, key, inputStream, objectMetadata);

    //您可以使用下面两种方式为上传的文件指定ACL，后一种已被注释
        putObjectrequest.setCannedAcl(CannedAccessControlList.PublicReadWrite);
        PutObjectResult putObjectResult = s3.putObject(putObjectrequest);
        //an-static/com-xx-xx/feedback/64位唯一字符串.后缀.
//        URL url = s3.generatePresignedUrl(Bsybean.bucketName, key,
//                new Date(126, 11, 17)); //2016年12月17日过期
//        System.out.println("fileUrl========"+url.toString());
        return key;
    }

    /**
     * 将图片转换成二进制
     * @return
     */
    public static byte[] readStream(String imagepath) throws Exception {
        FileInputStream fs = new FileInputStream(imagepath);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while (-1 != (len = fs.read(buffer))) {
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        fs.close();
        return outStream.toByteArray();
    }

}
